<?php namespace web\home\controller;
/** .-------------------------------------------------------------------
 * |  Software: [HDPHP framework]
 * |      Site: www.hdphp.com
 * |-------------------------------------------------------------------
 * |    Author: 向军 <2300071698@qq.com>
 * |    WeChat: aihoudun
 * | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
 * '-------------------------------------------------------------------*/

class Entry extends Conf_common {
	//首页
	public function index() {
		
//		热门推荐
		$re1=$this->getAdd($aid=0);
//		p($re1);die;
		View::with('re1',$re1);
		
//		获取品牌数据
		$brandData=Db::table('brand')->get();
//		p($brandData);die;
		View::with('brandData',$brandData);
		
		
		
		
		
		$Data=array(
			$this->getData($cid=1,$click=1),
			$this->getData($cid=18,$click=1),
			$this->getData($cid=19,$click=1),
			$this->getData($cid=20,$click=1),
			$this->getData($cid=21,$click=1),
		);
//		p($Data);die;
		View::with('Data',$Data);	
		View::make();
	}
	
//	热门广告
	public function getAdd($aid){
		return $re=Db::table('addlist')->where('aid',$aid)->get();
	}
	
	
//	获取指定分类下的数据
	public function getData($cid,$click=0){
//		获取所有分类数据
		$cidData=Db::table('category')->get();
//		获取当前分类的子级分类
		$Data=$this->getSon($cidData,$cid);
//		压入自己
		$Data[]=$cid;	
//		获取分类下的所有数据
//		如果$click为真就按点击排序			
		if($click){
			$goodsData['shuju']=Db::table('goods')->where("cid in(".implode(',',$Data).")")->orderBy('click','DESC')->get();
		}else{
			$goodsData['shuju']=Db::table('goods')->where("cid in(".implode(',',$Data).")")->get();
		}
//		获取指定分类下的商品品牌
		foreach($goodsData['shuju'] as $k=>$v){
			$goodsData['brand'][]=current(Db::table('brand')->where('bid',$v['bid'])->groupBy('bid')->get());
		}
//		p($goodsData['brand']);die;
		if($goodsData['brand']){foreach($goodsData['brand'] as $k=>$v){
			$temp[]=join(',',$v);
		}
		$goodsData['brand']=$temp;
		$goodsData['brand']=array_unique($goodsData['brand']);
		foreach($goodsData['brand'] as $k=>$v){
			$arr=explode(',',$v);
			$temp2[$k]['bid']=$arr[0];
			$temp2[$k]['bname']=$arr[1];
			$temp2[$k]['logo']=$arr[2];
			$temp2[$k]['sort']=$arr[3];
			$temp2[$k]['ishot']=$arr[1];
		}
		$goodsData['brand']=$temp2;}
		$goodsData['cid']=$cid;
		$goodsData['cate']=Db::table('category')->where('cid',$cid)->pluck('cname');
//		p($goodsData);
		return $goodsData;
	}
	
//	递归获取子级
	public function getSon($data,$cid){
		$cidData=array();
//		获取所有分类ID
		foreach($data as $k=>$v){
			if($cid==$v['pid']){
				$cidData[]=$v['cid'];
				$cidData=array_merge($cidData,$this->getSon($data,$v['cid']));
			}
		}
		return $cidData;
	}
	
	
	
	
}